﻿using System;
using PTiRouZ.Net.SilverLight.Api;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;

namespace PTiRouZ.Net.SilverLight.Api.HelloTipi
{
    /// <summary>Objet du résultat d'une requête d'envoie de message privée</summary>
    /// <see cref="update"></see>
    //[DataContract]
    //public class dm_new : direct_messages { }
    
    public delegate void dmNewEventHandler(object sender, dmNewEventArgs e);
    public class dmNewEventArgs : EventArgs
    {
        public dmNewEventArgs(direct_messages dmn)
        {
            _result = dmn;
        }

        private direct_messages _result;
        public direct_messages result { get { return _result; } }
    }

    public partial class apitxt : api
    {

        public event dmNewEventHandler dmNewCompleted;       

        #region Direct_Message New
        /// <summary>Permet de poster un nouveau message privé dont l’expéditeur est l’utilisateur authentifié.</summary>
        /// <c>http://www.hellotipi.com/dev/documentation/fonctions-messages-textes/#direct_messages/new</c>
        /// <param name="user">login du destinataire du message</param>
        /// <param name="text">texte du message à envoyer, 500 caractères max</param>        
        public void dmNew(string user, string text)
        {
            base.APICallCompleted += new APICallCompletedEventHandler(dmNewCallCompleted);

            var querystring = new List<KeyValuePair<string, string>>
                             {
                                 new KeyValuePair<string, string>("user", user),
                                 new KeyValuePair<string, string>("text", text)
                             };

            PostUrlApi("direct_messages/new.json", querystring);
        }

        /// <summary>Permet de poster un nouveau message privé dont l’expéditeur est l’utilisateur authentifié.</summary>
        /// <c>http://www.hellotipi.com/dev/documentation/fonctions-messages-textes/#direct_messages/new</c>
        /// <param name="user">id du destinataire du message</param>
        /// <param name="text">texte du message à envoyer, 500 caractères max</param>             
        public void dmNew(int user, string text)
        {
            base.APICallCompleted += new APICallCompletedEventHandler(dmNewCallCompleted);

            var querystring = new List<KeyValuePair<string, string>>
                             {
                                 new KeyValuePair<string, string>("user", user.ToString()),
                                 new KeyValuePair<string, string>("text", text)
                             };

            PostUrlApi("direct_messages/new.json", querystring);
        }
        
        
        void dmNewCallCompleted(object sender, ApiOpenReadCompletedEventArgs e)
        {
            try
            {
                dmNewCompleted.Invoke(sender, new dmNewEventArgs(serializeTools.Deserialize<direct_messages>(e.Result)));
            }
            finally
            {
                base.APICallCompleted -= new APICallCompletedEventHandler(dmNewCallCompleted);
            }
        }

        #endregion
    }

}

